//
//  DestinationCell.m
//  MyTrip
//
//  Created by zhang on 15/8/24.
//  Copyright (c) 2015年 张益. All rights reserved.
//

#import "DestinationCell.h"
#import "CityCell.h"
#import "Destination.h"
#import "DKNightVersion.h"

@interface DestinationCell ()<UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation DestinationCell

- (void)dealloc
{
    [_indexCollection release];
    [_indexArray release];
    [super dealloc];
}

- (instancetype) initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.indexArray = [NSMutableArray array];
        [self createCollection];
        
    }
    return self;
}

- (void)createCollection{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.frame.size.width / 2 - 5 * OffWidth, self.frame.size.height / 2 - 5 * OffHeight);
    flowLayout.sectionInset = UIEdgeInsetsMake(3 * OffHeight, 3 * OffWidth, 3 * OffHeight, 3 * OffWidth);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    self.indexCollection = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout] autorelease];
    _indexCollection.delegate = self;
    _indexCollection.dataSource = self;
    [_indexCollection registerClass:[CityCell class] forCellWithReuseIdentifier:@"city"];
    [_indexCollection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.contentView addSubview:_indexCollection];
    [_indexCollection setBackgroundColor:[UIColor whiteColor]];
//    _indexCollection.nightBackgroundColor = UIColorFromRGB(0x343434);
    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _indexArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CityCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"city" forIndexPath:indexPath];
    Destination *des = [_indexArray objectAtIndex:indexPath.item];
    [cell setValueWithDestination:des];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    Destination *des = [_indexArray objectAtIndex:indexPath.item];
    
    [self.delegate receiveIndex:indexPath WithType:des.type idNum:des.idNum destination:des];
    
}






@end
